package cn.zl.ymcc.service.impl;

import cn.zl.ymcc.constant.MqConstant;
import cn.zl.ymcc.entity.CourseOrder;
import cn.zl.ymcc.mapper.CourseOrderMapper;
import cn.zl.ymcc.service.CourseOrderService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import io.swagger.v3.oas.annotations.tags.Tag;

import java.math.BigDecimal;
import java.time.LocalDateTime;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zl
 * @since 2025-10-31
 */

@Tag(name = "CourseOrder服务实现")

@Service
public class CourseOrderServiceImpl extends ServiceImpl<CourseOrderMapper, CourseOrder> implements CourseOrderService {

    @Autowired
    private TransactionMQProducer transactionMQProducer;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void generateOrder(Long courseId) throws MQClientException {
        CourseOrder courseOrder = new CourseOrder();
        Long orderNo = System.currentTimeMillis();
        courseOrder.setOrderNo(orderNo.toString());
        courseOrder.setUserId(1L);
        //订单状态 ：	    //0下单成功待支付，	    //1支付成功订单完成	    //2用户手动取消订单(未支付)	    //3.支付失败	    //4.超时自动订单取消
        courseOrder.setStatusOrder(0);
        courseOrder.setPayType(0);
        courseOrder.setTotalCount(1);
        courseOrder.setTitle("课程");
        courseOrder.setVersion(0);
        courseOrder.setCreateTime(LocalDateTime.now());
        courseOrder.setUpdateTime(LocalDateTime.now());
        //订单价格
        courseOrder.setTotalAmount(new BigDecimal(0));
        Message message = new Message(
                MqConstant.MQ_TOPIC_ORDER,
                MqConstant.MQ_TAGS_COURSEORDER_PAYORDER,
             JSONObject.toJSONBytes(courseOrder)  );
        message.setTransactionId(courseOrder.getOrderNo());
        TransactionSendResult transactionSendResult = transactionMQProducer.sendMessageInTransaction(
                message,
                courseOrder

        );

        System.out.println(transactionSendResult);
    }

    @Override
    public boolean processOrderPayment(String messageBody, Object arg) {
        CourseOrder courseOrder = JSONObject.parseObject(messageBody, CourseOrder.class);
        if (courseOrder==null)
            return false;
        save(courseOrder);
        return true;
    }

    @Override
    public boolean checkTransactionStatus(String transactionId, String messageBody) {
        CourseOrder courseOrder = getById(transactionId);
        if (courseOrder!=null)
            return true;
        return false;
    }
}
